#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by ZhangYichao on 2018/8/9

# 首先查找默认文件夹下有多少文档需要整合
import glob
import json
import xlrd
from collections import OrderedDict


fileLocation =u"E:\\cs\\01work\\嘉和美康\\青岛大学附属医院\\大数据课题\\数据交换模板示例\\"
checkFile="ColumnCounts"
file = open(checkFile)
dictColumnCounts = json.loads(file.read(),object_pairs_hook=OrderedDict);

if __name__=="__main__":
    print(fileLocation)

    print("在默认文件夹下应有%d个文档" % len(dictColumnCounts))
    #print(dictColumnCounts)
    for  k in dictColumnCounts.keys():
        #print(k)
        fName = k
        filePathName=fileLocation+fName
        bk=xlrd.open_workbook(filePathName)
        try:
            sh=bk.sheet_by_name("Sheet1")
        except:
            print ("在文件%s中没有找到[Sheet1]" % filePathName)
        result=sh.ncols==dictColumnCounts[k]
        msg = "文件 {0:30}中实际有 {1:5}列，应有 {2:5}列，结果：{3:5}".format(fName,
                                                                   sh.ncols, dictColumnCounts[k], str(result))
        msgFals = msg + " 差值：[{0}]".format(dictColumnCounts[k] - sh.ncols)
        if result:
            print(msg)
        else:
            print("\033[0;31m"+msgFals+ "\033[0m")
